package ecoll.client.centers;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import ecoll.client.Client;
import ecoll.client.database.News;

public class NewsCenter implements Center {

	@Override
	public String name() {
		return "Main page";
	}

	@Override
	public void execute() {
		final VerticalPanel newsList = new VerticalPanel();
		final ScrollPanel newsPanel = new ScrollPanel(newsList);
		newsList.setWidth("100%");

		Client.DATABASE_SERVICE.getNews(new AsyncCallback<ArrayList<News>>() {
			public void onFailure(Throwable error) {
				Window.alert("News fetching failure");
			}

			public void onSuccess(ArrayList<News> result) {

				for (int i = 0; i < result.size(); ++i) {
					VerticalPanel panel = new VerticalPanel();
					Label label = new Label(result.get(i).getText() + " on "
							+ result.get(i).getDate());
					panel.add(label);
					if (i % 2 == 1)
						panel.setStyleName("evenLabel");
					else
						panel.setStyleName("oddLabel");
					newsList.add(panel);
				}

				Client.updateDashboard(newsPanel);
			}
		});

	}

	@Override
	public Function function(int i) {
		return null;
	}

}
